package com.newtouch.cq.lang.result;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.newtouch.cq.lang.em.BaseExceptionEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.List;


/**
 * 分页返回对象
 *
 * @param <T>
 * @author Alex
 */
@Getter
@Setter
@Accessors(chain = true)
@ApiModel("返回对象")
public class MccPageResult<T>  extends MccResult{
    @ApiModelProperty(value = "data",example = "{\"records\":[]}")
    protected Page<T> data;

    private MccPageResult(List<T> content, IPage<T> pageable, long total) {
        super(BaseExceptionEnum.SUCCESS);
        Page<T> page = Page.of(pageable.getCurrent(), pageable.getSize(), total);
        page.setRecords(content);
        this.data = page;
    }

    public static <E> MccPageResult<E> success(IPage<E> pageData) {
        return success(pageData.getRecords(), pageData, pageData.getTotal());
    }

    public static <E> MccPageResult<E> success(int current, int size) {
        return success(null,current,size,0);
    }

    public static <E> MccPageResult<E> success(List<E> content, int current, int size, long total) {
        if (ObjectUtil.isNull(content)) {
            content = CollUtil.newArrayList();
        }
        return success(content, new PageDTO(current, size), total);
    }

    public static <E> MccPageResult<E> success(List<E> content, IPage<E> pageable, long total) {
        return new MccPageResult<E>(content, pageable, total);
    }
}
